OB.Emes_ActionButton = OB.Emes_ActionButton || {};

var MaintainWin = null;

//设备保养
OB.Emes_ActionButton.Facility_Maintain = function(params, win) {
	var grid = win.activeView.viewGrid;
	var record = grid.getSelectedRecord();
	
	MaintainWin = win;
	
	if(record._entityName == 'EMES_Maintain_Plan'){ //点击计划打开
		var requestData = {type:"OBPOPWINDOWPLAN",code:record.id}
	}else{ 
		var requestData = {type:"OBPOPWINDOWITEM",code:record.id}
	}
	//初始化先执行查询数据
	OB.RemoteCallManager.call("com.mes.handler.EmesoFacilitiesCollectionHandler",requestData,{},function(response, data, request){
		if(data && data.flag){
			isc.EMESO_Facilities_Manager.create(data).show();
		}else{
			isc.showMessage(data.desc,isc.OBMessageBar.TYPE_INFO);
		}
	});
	
};



/**
 * 设备维保
 */
isc.defineClass("EMESO_Facilities_Manager", isc.OBPopup).addProperties({
	title: "设备管理",
	width: '80%',
	height: '80%',
	align: 'center',
	showMinimizeButton : false,
	showMaximizeButton : false,
	defaultLayoutAlign: 'center',
	keyDown:function(event){
		if(event.nativeKeyCode==13){
			queryForm.search();
			return false;
		}
		return true;
	},
	Fancility : {},        //设备
	Fancility_Map : {},        //设备维保项目map
	Plan_Map : [],   //维保计划select
	MaintainList:[],
	requestData:null,              //统一提交条件
	formData:{},
	orderId: null,
	submit: function(isForce){
		alert("提交代码未写！");
	},
	clearForm: function(form){
		
	},
	
	clearValue: function(form) {
		if(form.getItem('group1')!=null){
			var group1 = form.getItem('group1').itemIds;
			while(group1.length != 0){
				var name = group1[0];
				if(name != 'group1' && form.getField(name)){
					form.removeItem(form.getItem(name));
					group1.remove(name);
				}
			}
		}
		if(form.getItem('group')!=null){
			var group = form.getItem('group').itemIds;
			while(group.length != 0){
				var name = group[0];
				if(name != 'group' && form.getField(name)){
					form.removeItem(form.getItem(name));
					group.remove(name);
				}
			};
		}
	},
	
	
	changeValue:function(data){
		this.Facilities_Form.getField("Facility_Current_Code").setValue(data.code);
		this.Facilities_Form.getField("Facility_Details").setValue(data.name);
		this.Facilities_Form.getField("Facility_Status").setValue(data.status);
		this.Facilities_Form.getField("Facility_Type").setValue(data.type);
		this.Facilities_Form.getField("Facility_Org").setValue(data.org);
		this.Facilities_Form.getField("Facility_User").setValue(data.user);
	},
	
	//cancel function
	closeFunction:function(){
		this.close();
	},
	
	
	//界面追加字段
	formAddFiled:function(form, data,readOnly){
		if(data.length == 0){
			this.Maintain_Form.hide();
			this.Repair_Form.hide();
			this.No_data_Form.show();
			this.No_data_Form.getItem('Warn_Message').setValue('该设备下，暂无保养项目。');
		}else{
		this.No_data_Form.hide();
		var group = form.getItem("group").itemIds;
		for(var i = 0 ; i < data.length ; i ++){
			var name = data[i].code;
			group.push(name);
			group.push(data[i].id+"_checkbox");
			group.push(data[i].id+"_desc");
			form.addField({paramId:data[i].id , value:data[i].name , type: 'text' , readOnlyDisplay : "static",canEdit:false , title:'保养项目' , name:data[i].code , width: '*' , length: 20 , startRow:true});
			form.addField({name:data[i].id+"_checkbox" , title: "勾选完成", type: "boolean", align:"left" });
			form.addField({name:data[i].id+"_desc", value:"" , type: 'text' , title:'保养说明' , width: '*' ,colSpan:3, startRow:false})
		}
		}
	},
	
	//界面追加字段
	formAddFiled1:function(form, data,readOnly){
		if(data.length == 0){
			this.Maintain_Form.hide();
			this.Repair_Form.hide();
			this.No_data_Form.show();
			this.No_data_Form.getItem('Warn_Message').setValue('该设备下，暂无保养项目。');
		}else{
		this.No_data_Form.hide();
		var group = form.getItem("group1").itemIds;
		for(var i = 0 ; i < data.length ; i ++){
			var name = data[i].code;
			group.push(name);
			group.push(data[i].id+"_checkbox");
			group.push(data[i].id+"_desc");
			form.addField({paramId:data[i].id , value:data[i].name , type: 'text' ,readOnlyDisplay : "static", canEdit:false , title:'维修项目' , name:data[i].code , width: '*' , length: 20 , startRow:true});
			form.addField({name:data[i].id+"_checkbox" , title: "勾选完成", type: "boolean", align:"left" });
			form.addField({name:data[i].id+"_desc", value:"" , type: 'text' , title:'维修说明' , width: '*' ,colSpan:3, startRow:false})
		}
		}
	},
	
	
	initWidget: function (data) {
		
	 formData = data;
	 
	 //设备
	 Fancility = [];
	
	 //设备信息
	 Fancility_Map = [];
	 
	 //动作类型
	 Action_Map = ['保养','维修'];
	
	 //计划信息
	 Plan_Map = [];
	
	 //保养项目详情
	 MaintainList = [];
	 
	 //维修项目详情
	 RepairList = [];
		
		this.width = document.body.clientWidth * 0.6;
		this.height = document.body.clientHeight * 0.6;
		
		
		var _self = this;
		//OB控件 追加	addField or addItem	会有样式和事件找不到的问题
		_self.Action_Map = ['保养','维修'];
		
			
			_self.Fancility_Map = [];
			_self.MaintainList = [];
			_self.RepairList = [];
			_self.Plan_Map = [];
			
			for(var i = 0;i<formData.fancility.length;i++){
				_self.Fancility_Map.push(formData.fancility[i].code);
			}
			
			_self.MaintainList = formData.fancility[0].maintainList;
			_self.RepairList = formData.fancility[0].repairList
			_self.Fancility = formData.fancility[0];
			_self.Plan_Map.push(formData.code);
			
		
		//头信息
		this.Head_Form = isc.DynamicForm.create({
			ID: "Head_Form",
			width:this.width,
			height:this.height * 0.1,
			numCols : 8,
			defaultLayoutAlign: 'center',
			
			//给select赋值
			addDataToSelect:function(flag){
			
			_self.Plan_Map = [];
			_self.Plan_Map.push(formData.code);
			_self.Head_Form.getItem("Head_Form_Plan").setValueMap(_self.Plan_Map);
			
			
				_self.clearValue(_self.Repair_Form);
				_self.clearValue(_self.Maintain_Form);
				
				var actionType = Head_Form.getValue('Head_Form_Action');
				
				for(var i= 0 ;i<formData.fancility.length ; i++){
					
					if(formData.fancility[i].code == flag){
						if(actionType == '保养'){
							_self.Fancility = formData.fancility[i];
							_self.MaintainList = formData.fancility[i].maintainList;
							_self.formAddFiled(_self.Maintain_Form,_self.MaintainList,true);
						}else{
							_self.Fancility = formData.fancility[i];
							_self.RepairList = formData.fancility[i].repairList;
							_self.formAddFiled1(_self.Repair_Form,_self.RepairList,true);
						}
					}else{
						continue;
					}
				}
				_self.changeValue(_self.Fancility);
			},
			
			//给Repair_Form select赋值
			addDataToSelect1:function(flag){
				
				_self.Plan_Map = [];
				_self.Plan_Map.push(formData.code);
				_self.Head_Form.getItem("Head_Form_Plan").setValueMap(_self.Plan_Map);
				_self.clearValue(_self.Repair_Form);
				_self.clearValue(_self.Maintain_Form);
				
				
				var facilityCode = Head_Form.getValue('Head_Form_Fancility');
				
				for(var i= 0 ;i<formData.fancility.length ; i++){
					
					if(formData.fancility[i].code == facilityCode){
						if(flag == '保养'){
							_self.Repair_Form.hide();
							_self.Maintain_Form.show();
							_self.Fancility = formData.fancility[i];
							_self.MaintainList = formData.fancility[i].maintainList;
							_self.formAddFiled(_self.Maintain_Form,_self.MaintainList,true);
						}else{
							_self.Repair_Form.show();
							_self.Maintain_Form.hide();
							_self.Fancility = formData.fancility[i];
							_self.RepairList = formData.fancility[i].repairList;
							_self.formAddFiled1(_self.Repair_Form,_self.RepairList,true);
						}
						
					}else{
						continue;
					}
				}
				_self.changeValue(_self.Fancility);
			},
			
			
			fields : [
			        {width: '*',type: 'select',title:'任务编码',	    name: 'Head_Form_Plan',		  valueMap:_self.Plan_Map, defaultValue:_self.Plan_Map[0] , readOnlyDisplay:"static",startRow:true},
			        {width: '*',type: 'select',title:'设备/磨具编码',     name: 'Head_Form_Fancility',  valueMap:_self.Fancility_Map , defaultValue:_self.Fancility_Map[0] , readOnlyDisplay:"static",startRow:false,
			        	changed:"Head_Form.addDataToSelect(Head_Form.getValue('Head_Form_Fancility'));"},
			        {width: '*',type: 'select',title:'操作类型', name: 'Head_Form_Action',  valueMap:_self.Action_Map , defaultValue:_self.Action_Map[0] , readOnlyDisplay:"static",startRow:false,
			        	changed:"Head_Form.addDataToSelect1(Head_Form.getValue('Head_Form_Action'));"}
			    ]
		});
		
		
		//设备详情页面
		this.Facilities_Form = isc.DynamicForm.create({
			ID: "Facilities_Form",
			width:this.width,
			height:this.height * 0.35,
			numCols : 8,
			defaultLayoutAlign: 'center',
			fields : [
					{width: '*', name:'group',type: 'OBSectionItem',defaultValue:'设备/工模信息',sectionExpanded: true,colSpan:8,itemIds: ['Facility_Current_Code','Facility_Details','Facility_Status','Facility_Type','Facility_Org','Facility_User']},
			        {width: '*',type: 'text', length: 32,title:'设备/工模编号',		    name: 'Facility_Current_Code', value:Fancility.code,   canEdit:false, startRow:true},
				    {width: '*',type: 'text', length: 32,title:'名称-规格',		    name: 'Facility_Details',	   value:Fancility.name,   canEdit:false, startRow:false},
				    {width: '*',type: 'text', length: 32,title:'状态',		        name: 'Facility_Status',	   value:Fancility.status, canEdit:false, startRow:false},
			        {width: '*',type: 'text', length: 32,title:'类别',				name: 'Facility_Type',	       value:Fancility.type,   canEdit:false, startRow:true},
			        {width: '*',type: 'text', length: 32,title:'隶属工段',		        name: 'Facility_Org',		   value:Fancility.org,    canEdit:false, startRow:false},
			        {width: '*',type: 'text', length: 32,title:'责   任  人',		        name: 'Facility_User',		   value:Fancility.user,   canEdit:false, startRow:false}
			    ]
		});
		
		
		
		this.Maintain_Form = isc.DynamicForm.create({
			ID: "Maintain_Form",
		
			MaintainPlanMap:null,
			width:this.width ,
			height:this.height * 0.35,
			defaultLayoutAlign: 'center',
			numCols : 8,
			
			fields : [
					{width: '*',name:'group',type: 'OBSectionItem',defaultValue:'设备/工模保养信息',sectionExpanded: true,colSpan:8,itemIds: []},
			        ]
		});
		
		this.Repair_Form = isc.DynamicForm.create({
			ID: "Repair_Form",
		
			RepairPlanMap:null,
			width:this.width ,
			height:this.height * 0.35,
			defaultLayoutAlign: 'center',
			numCols : 8,
			
			fields : [
					{width: '*',name:'group1',type: 'OBSectionItem',defaultValue:'设备/工模维修信息',sectionExpanded: true,colSpan:8,itemIds: []},
			        ]
		});
		
		this.No_data_Form = isc.DynamicForm.create({
			ID: "No_data_Form",
			RepairPlanMap:null,
			width:this.width ,
			height:this.height * 0.35,
			defaultLayoutAlign: 'center',
			numCols : 8,
			fields : [
					{width: '*',name:'group1',type: 'OBSectionItem',defaultValue:'提示信息',sectionExpanded: true,colSpan:8,itemIds: ['Warn_Message']},
					{width: '*',type: 'text', length: 60,title:'提示信息',		        name: 'Warn_Message',		   value:'',   canEdit:false, startRow:false}
			        ]
		});
		
		
		_self.changeValue(_self.Fancility);
		_self.Repair_Form.hide();
		_self.No_data_Form.hide();
		this.formAddFiled(this.Maintain_Form,_self.MaintainList,true);
		
		// OK Button
		this.okButton = isc.OBFormButton.create({
			title : '确定',
			popup : this,
			action : function() {
				
			var actionType = Head_Form.getValue('Head_Form_Action');
			
			var SelectData = [];
			var SelectDataSource = [];
			var SelectForm = null;
			
			if(actionType == '维修'){
				SelectDataSource = _self.RepairList;
				SelectForm = _self.Repair_Form;
			}else{
				SelectDataSource = _self.MaintainList;
				SelectForm= _self.Maintain_Form;
			};
			
			for(var i = 0;i<SelectDataSource.length;i++){
				var column1 = SelectForm.getField(SelectDataSource[i].id+'_checkbox');
				var column2 = SelectForm.getField(SelectDataSource[i].id+'_desc');
				if(column1.getValue()){
					SelectData.push(SelectDataSource[i].id+'@@'+column2.getValue());
				}
		    };
			
			if(SelectData.length == 0 ){isc.showMessage("如有项目已经维护完成，请勾选。否则请点击取消按钮",isc.OBMessageBar.TYPE_INFO); return; };
			
			requestData = {};
		    requestData = {action:actionType , type:"OKBUTTON" , code:SelectData.join("#####")};
			//初始化先执行查询数据
			OB.RemoteCallManager.call("com.mes.handler.EmesoFacilitiesCollectionHandler",requestData,{},function(response, data, request){
				if(data && data.flag){
					_self.closeFunction();
					MaintainWin.refresh();
					isc.showMessage(data.message,isc.OBMessageBar.TYPE_INFO);
				}else{
					isc.showMessage(data.message , isc.OBMessageBar.TYPE_INFO);
				}
			});
			}
		});
		
		// Cancel Button
		this.cancelButton = isc.OBFormButton.create({
			title : '取消',
			popup : this,
			action : function() {
			_self.closeClick();
			}
		});
		
		
		this.items = [
		  			isc.VLayout.create({
		  				width: '100%',
		  				height:'100%',
		  				layoutMargin : this.height * 0.1,
		  				membersMargin : 0,
		  				members : [
		  					isc.VLayout.create({
		  						width: this.width,
				  				height:this.height * 0.8,
		  						defaultLayoutAlign : "center",
		  						align : "center",
		  						membersMargin : 10,
		  						members : [this.Head_Form,this.Facilities_Form, this.Maintain_Form,this.Repair_Form,this.No_data_Form ]
		  					}) ,
		  					isc.HLayout.create({
		  						width: this.width,
				  				height:this.height * 0.2,
		  						defaultLayoutAlign : "center",
		  						align : "center",
		  						membersMargin : 10,
		  						members : [ this.okButton, this.cancelButton ]
		  					}) 
		  				]
		  			})
		  		];
		this.Super("initWidget", arguments);
	}
});
